/*
 * display blog post in a specific date
 * @method displayBlogByDate
 * @param {String}date the date selected in the calendar
 */
function displayBlogByDate(date) {
	
	$("#content").html("");
	
	// find blog posts of the date
	var length = 0;	// track the blog number
	for(var i in blogposts) {
		if(blogposts[i].date == date) {
			appendPost(blogposts[i]);
			length++;
		}
	}
	
	// if no match, show a message
	if (length == 0) {
		$("#content").prepend("<p>No post in this day.</p>");
	}
	
	// showing the title of selection
	$("#content").prepend("<h3>Blog of " + date + "</h3>" +
			"<a id='return' href='#'>Return to all posts&lt;&lt;</a>" +
			"<div class='cleaner_h20'></div>");
	
	// activate edit and delete button
	eventHandler();
}

/**
 * to render the blog calendar
 * @method calendar()
 */
function yui_calendar() {
	YUI().use('calendar', function (Y) {

		  Y.one('body').addClass('yui3-skin-sam');

		  // render the calendar
		  var calendar = new Y.Calendar({
		          contentBox: "#calendar_div",
		          height:'200px',
		          width:'200px',
		          showPrevMonth: true,
		          showNextMonth: true,
		          date: new Date()
		  }).render();
		  
		  // set custom style: highlight date with posts
		  var selectedDate = new Array("", "", "", "");
		  for(var i in blogposts) {
			  var date = blogposts[i].date.split("-");
			  switch(date[1])
			  {
			  case "01":
				  selectedDate[0] += date[2];
				  selectedDate[0] += ",";
				  break
			  case "02":
				  selectedDate[1] += date[2];
				  selectedDate[1] += ",";
				  break
			  case "03":
				  selectedDate[2] += date[2];
				  selectedDate[2] += ",";
				  break
			  case "04":
				  selectedDate[3] += date[2];
				  selectedDate[3] += ",";
				  break
			  default:
				  break
			  }
		  }
//		  var rules = {
//				  "2012": {
//					  "0": {
//						  selectedDate[0]: "selected-date"
//					  },
//					  "1": {
//						  selectedDate[1]: "selected-date"
//					  },
//					  "2": {
//						  selectedDate[2]: "selected-date"
//					  },
//					  "3": {
//						  selectedDate[3]: "selected-date"
//					  }
//				  }
//		  };
		  
//		  var filterFunction = function (date, node, rules) {
//			  if (rules.indexOf("selected-date") >= 0)
//			  {
//				  node.addClass("selected-date");
//			  }
//		  
//		  }
//		  
//		  calendar.set("customRenderer", {rules: rules, filterFunction: filterFunction});
		  
		  // get a reference to Y.DataType.Date
		  var dtdate = Y.DataType.Date;
		  
		  // listen to calendar's selectionChange event
		  calendar.on("selectionChange", function(ev) {
			  var newDate = dtdate.format(ev.newSelection[0]);
			  displayBlogByDate(newDate);
		  });

	});
}

/*
function yui_autocomplete() {	
	YUI().use('autocomplete', 'autocomplete-filters', 'autocomplete-highlighters', function (Y) {
		
		Y.one('#create_user').plug(Y.Plugin.AutoComplete, {
			resultFilters: 'Blogs',
			//resultHighlighter: 'phraseMatch',
			source: blogusers
		});
	});
}*/